// 导入api接口
import configJs from '@/utils/configJs'
import defaultData from './data'
import { mapState, mapGetters, mapActions } from 'vuex'
import { userAuthList, userAuthUpdate, userFreeze, userUnfreeze, userUpdate } from '@/api/api'
var data = JSON.parse(JSON.stringify(defaultData))
var adapter = {
	data,
	methods: {
		// ...mapActions(['logOut']),
		/**
		 * 初始化
		 */
		initPage(){
			//还原页面对象
			configJs.revert(data, defaultData);
		},
		/**
		 * 全选事件
		 */
		selectAll(key) {
			for(let i in this.authObj[key]) {
				if (i.indexOf('authority') > -1) {
					if (key == 'warnDetails') {
						if (['authorityAdd','authorityDelete','authorityImport','authorityUpload'].indexOf(i) > -1 ) {
							continue
						} else {
							this.authObj[key][i] = true
						}
					}
					this.authObj[key][i] = true
				}
			}
		},

		/**
		 * 反选事件
		 */
		cancelAll(key) {
			for(let i in this.authObj[key]) {
				if (i.indexOf('authority') > -1) {
					if (key == 'warnDetails') {
						if (['authorityAdd','authorityDelete','authorityImport','authorityUpload'].indexOf(i) > -1 ) {
							continue
						} else {
							this.authObj[key][i] = false
						}
					}
					this.authObj[key][i] = false
				}
			}
		},

		/**
		 * 清除
		 */
		clearAll() {
			for(let j in this.authObj) {
				for(let i in this.authObj[j]) {
					if (i.indexOf('authority') > -1) {
						this.authObj[j][i] = false
					}
				}
			}
		},
		/**
		* 查询权限
		*/
		async getAuth() {
			const res = await userAuthList()
			if (res.code === 0) {
				// this.$set(this, 'userList', res.data.userList)
			  this.userList = res.data.userList
				this.authList = res.data.userAuthorityList
			} else {
				this.$message(res.msg)
			}
		},

		/**
		 * 选择用户点击
		 */
		selectUser(id) {
			this.activeId = id
			this.activeUser = this.userList.reduce((total,item) => {
				if (item.id == id) {
					total = {...item}
					this.activeUserIsAdmin = item.isAdmin
				}
				return total
			}, {})
			this.clearAll()
			this.filterAuthByUser()
		},

		/**
		 * 筛选用户对应权限
		 */
		filterAuthByUser() {
			this.userAuthList = this.authList.filter(item => {
				if (item.userid == this.activeId) {
					return true
				} else {
					return false
				}
			})
			this.setPageAuth()
		},

		/**
		 * 改变页面状态
		 */
		setPageAuth() {
			for (let i = 0; i < this.userAuthList.length; i++) {
				let auth = this.userAuthList[i]
				let authUrl = auth.menuUrl
				for(let j in auth) {
					if (this.authObj[authUrl].hasOwnProperty(j)) {
						this.authObj[authUrl][j] = auth[j]
					}
				}
				
			}
		},

		/**
		 * 筛选出修改过的权限
		 */
		getChangeAuth() {
			console.error('getChangeAuth')
			// 原有权限数组转换为对象
			let userAuthBefore = this.userAuthList.reduce((total,item) => {
				total[item.menuUrl] = item
				return total
			}, {})
			console.error('userAuthBefore')
			console.log(userAuthBefore)
			for(let url in this.authObj) { // 遍历authObj
				if (userAuthBefore.hasOwnProperty(url)) { // 判断原有的权限中是否包含这组权限
					for(let auth in this.authObj[url]) { // 遍历页面对应的权限组中每个权限
						if (auth.indexOf('authority') > -1) { // 过滤出权限
							if(this.authObj[url][auth] == true) { // 如果此项权限被勾选，直接添加进原有权限中。如果没有勾选，先判断原有权限中有没有这项权限，有就添加进原有权限
								userAuthBefore[url][auth] = true
							} else {
								if (userAuthBefore[url].hasOwnProperty(auth)) {
									userAuthBefore[url][auth] = false
								}
							}
						}
					}
				} else {
					for(let auth in this.authObj[url]) { // 遍历页面对应的权限组中每个权限
						if (auth.indexOf('authority') > -1) { // 过滤出权限
							if(this.authObj[url][auth] == true) { // 如果此项权限被勾选，直接添加进原有权限中。
								if (!userAuthBefore.hasOwnProperty(url)) {
									userAuthBefore[url] = {}
									userAuthBefore[url].menuUrl = url
									userAuthBefore[url].menuName = this.authObj[url].menuName
									userAuthBefore[url].userid = this.activeId
								}
								userAuthBefore[url][auth] = true
							}
						}
					}
				}
			}
			console.error('userAuthAfter')
			console.log(userAuthBefore)
			// 对象转换为数组
			let afterAuth = []
			for(let i in userAuthBefore){
				afterAuth.push(userAuthBefore[i])
			}
			console.error('afterAuth')
			console.log(afterAuth)
			return afterAuth
		},

		/**
		 * 修改用户权限
		 */
		async authUpdate() {
			if (this.isSelf) {
				return
			}
			let authorityUpdate = this.getChangeAuth()
			let params = authorityUpdate
			const res = await userAuthUpdate(params)
			if (res.code == 0) {
				this.$message('保存成功')
			} else {
				this.$message(res.msg)
			}
			if (this.activeUserIsAdmin !== this.activeUser.isAdmin) {
				// {id:this.activeUser.id,isAdmin:this.activeUser.isAdmin}
				const res1 = await userUpdate(this.activeUser)
				if (res1.code == 0) {
					this.activeUserIsAdmin = this.activeUser.isAdmin
				}
			}
		},

		/**
		 * 用户冻结
		 */
		async freeze(id) {
			if (this.isSelf) {
				return
			}
			await userFreeze(id)
			this.getAuth().then(() => {
				this.selectUser(id)
			})
		},

		/**
		 * 用户解冻
		 */
		async unfreeze(id) {
			if (this.isSelf) {
				return
			}
			await userUnfreeze(id)
			this.getAuth().then(() => {
				this.selectUser(id)
			})
		},

		/**
		* 修改
		*/
		checkChange() {},
	},
	computed: {
		...mapState({
			isAdmin: state => state.user.isAdmin,
			supperAdmin: state => state.user.supperAdmin,
			userId: state => state.user.id,
		}),
		...mapGetters(['userPageAuth']),
		isSelf() {
			return this.userId == this.activeUser.id
		},
	}
}
export default adapter
